﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyBet.Web.DAL;
using System.Web.Security;
using System.Web.Profile;
using System.ComponentModel.DataAnnotations;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace MyBet.Web.DAL
{
    [Serializable()]
    public class CustomContestObject
    {
        private List<ContestObject> _contestList;
        private string _displayName;
        private string _password;
        private int _id;

        [Key()]
        [DataMemberAttribute()]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [DataMemberAttribute()]
        public List<ContestObject> ContestList
        {
            get { return _contestList; }
            set { _contestList = value; }
        }

        [DataMemberAttribute()]
        public string DisplayName
        {
            get { return _displayName; }
            set { _displayName = value; }
        }

        [DataMemberAttribute()]
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public static CustomContestObject CopyFrom(CustomContest c)
        {
            CustomContestObject returnObject = new CustomContestObject();
            returnObject.Id = c.CustomContestId;
            returnObject.DisplayName = c.DisplayName;
            returnObject.Password = c.Password;
            return returnObject;
        }

        /// <summary>
        /// Get allowed contest for the given user
        /// </summary>
        /// <param name="userEmail"></param>
        /// <returns></returns>
        public static List<CustomContestObject> GetAllowedContests(MyBetDBEntities dbContext, string userEmail)
        {
            List<CustomContestObject> ccoList = new List<CustomContestObject>() ;
            var query1 = (from contest in dbContext.CustomContest
                         where contest.IsDefault==true
                         select contest);
            List<CustomContest> contests = query1.ToList();

            foreach (CustomContest cc in contests)
            {
                ccoList.Add(CustomContestObject.CopyFrom(cc));
            }

            var query2 = (from contest in dbContext.CustomContest
                         from contestLink in dbContext.UserMailCustomContest
                         where (contestLink.CustomContestId == contest.CustomContestId) && (contestLink.LoweredUserEmail == userEmail.ToLower())
                         select contest);
            contests = query2.ToList();

            foreach (CustomContest cc in contests)
            {
                ccoList.Add(CustomContestObject.CopyFrom(cc));
            }
            return ccoList;
        }

        /// <summary>
        /// Join given contest
        /// </summary>
        /// <param name="userEmail"></param>
        /// <returns></returns>
        public static bool JoinCustomContest(MyBetDBEntities dbContext, Guid userId, int customContestId)
        {
            int returnValue = -1;
            try
            {
                returnValue = dbContext.ExecuteStoreCommand("exec mybet_joinContest @UserId={0}, @CustomContestId={1}", userId, customContestId);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
                return false;
            }

            return (returnValue>0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userEmail"></param>
        /// <returns></returns>
        public static bool DisjoinCustomContest(MyBetDBEntities dbContext, Guid userId, int customContestId)
        {
            int returnValue = -1;
            try
            {
                returnValue = dbContext.ExecuteStoreCommand("exec mybet_disjoinContest @UserId={0}, @CustomContestId={1}", userId, customContestId);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
                return false;
            }

            return (returnValue > 0);
        }

    }
}